package com.mall.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public final class DateHelper {
	private static String year;
	private static String month;
	private static String day;
	private static String hour;
	private static String minute;
	private static String second;

	public static Date string2Date(String dateStr, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date string2DateCommon(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String date2StringCommon(Date date) {
		return date2String(date, "yyyy-MM-dd HH:mm:ss");
	}

	public static String date2String(Date date, String format) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static String date2StringNow(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (date == null) {
			return sdf.format(new Date());
		}
		return sdf.format(date);
	}

	public static Date toDayStart(Date date) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sdf.parse(sdf.format(date));
		} catch (ParseException e) {
		}
		return null;
	}

	public static Date toDayEnd(Date date) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date tmp = null;
		try {
			tmp = sdf.parse(sdf.format(date));
		} catch (ParseException e) {
			return null;
		}
		return new Date(tmp.getTime() + 86400000L - 1L);
	}

	public static void setCurrentDate() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		year = String.valueOf(cal.get(1));
		month = String.valueOf(cal.get(2) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		day = String.valueOf(cal.get(5));
		if (day.length() == 1) {
			day = "0" + day;
		}
		hour = String.valueOf(cal.get(10));
		if (hour.length() == 1) {
			hour = "0" + hour;
		}
		minute = String.valueOf(cal.get(12));
		if (minute.length() == 1) {
			minute = "0" + minute;
		}
		second = String.valueOf(cal.get(13));
		if (second.length() == 1) {
			second = "0" + second;
		}
	}

	public static final synchronized String getCurrentYear() {
		setCurrentDate();
		return year;
	}

	public static final synchronized String getCurrentMonDay() {
		setCurrentDate();
		return month + day;
	}

	public static final synchronized String getCurrentYearMonDay() {
		setCurrentDate();
		return year.substring(2) + month + day;
	}

	public static final synchronized String getLastYearMon() {
		setCurrentDate();
		if (Integer.parseInt(month) == 1)
			return getLastYear() + "12";
		String lastMonth = String.valueOf(Integer.parseInt(month) - 1);
		if (lastMonth.length() == 1)
			lastMonth = "0" + lastMonth;
		return year + lastMonth;
	}

	public static final synchronized String getLastYear() {
		setCurrentDate();
		int lastYear = Integer.parseInt(year) - 1;
		return String.valueOf(lastYear);
	}

	public static final synchronized String getCurrentMonth() {
		setCurrentDate();
		return month;
	}

	public static final synchronized String getCurrentYearMon() {
		setCurrentDate();
		return year + month;
	}

	public static final synchronized String getCurrentDate() {
		setCurrentDate();
		return year + "." + month + "." + day;
	}

	public static Timestamp now() {
		return new Timestamp(System.currentTimeMillis());
	}

	public static Date addDate(Date date, int dateSize) {
		Calendar cal = Calendar.getInstance();
		if (date == null) {
			date = new Date();
		}
		cal.setTime(date);
		int curDate = cal.get(5) + dateSize;
		cal.set(5, curDate);
		return cal.getTime();
	}
	
	/**
	 * 校验字符串是不是yyyy-MM-dd格式的
	 * @param dateStr
	 * @return
	 */
	public static boolean isDateFormat(String dateStr){
		if(StringUtils.isBlank(dateStr)){
			return false;
		}
		return Pattern.compile("\\d{4}-\\d{2}-\\d{2}").matcher(dateStr).matches();
	}

	public static void main(String[] args) {
	}
}